package com.lancoo.answer.view.fragment.webquesBody

import android.os.Bundle
import android.os.Handler
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.animation.TranslateAnimation
import androidx.fragment.app.Fragment
import com.lancoo.answer.R
import com.lancoo.answer.component.InteractWebView
import com.lancoo.answer.databinding.EvViewLookingQuesBodyBinding
import com.lancoo.answer.model.AnswerConstant
import com.lancoo.answer.model.ConstantBean
import com.lancoo.answer.model.bean.Item
import com.lancoo.answer.model.bean.Ques
import com.lancoo.answer.view.base.BaseVMFragment
import com.lancoo.answer.widget.combinationView.FillContextAnswerResultView

/**
 * Created by Dadong on 2021/12/24
 * Describe:带交互的题干查看数据
 * 词汇匹配
 * 听力填空等填空题也可以用
 */
class WebQuesBodyLookingFragment : BaseVMFragment<EvViewLookingQuesBodyBinding>() {

    private var typeIndex = 0
    private var quesIndex = 0
    private var childIndex = 0
    private var defaultLoadIndex = 0
    private lateinit var ques: Ques
    private var itemSortIndex: String = "" //当前选择的序号
    private var sortIndexType = "0" //抽题方式

    companion object {
        fun loadFragment(
            typeIndex: Int,
            quesIndex: Int,
            childIndex: Int,
            defaultLoadIndex: Int
        ): Fragment? {
            val mBundle = Bundle()
            mBundle.putInt(AnswerConstant.KEY_TYPEINDEX, typeIndex)
            mBundle.putInt(AnswerConstant.KEY_QUESINDEX, quesIndex)
            mBundle.putInt(AnswerConstant.KEY_CHILDINDEX, childIndex)
            mBundle.putInt(AnswerConstant.KEY_LOADCHILDINDEX, defaultLoadIndex)
            val checkQuesBodyFragment = WebQuesBodyLookingFragment()
            checkQuesBodyFragment.arguments = mBundle
            return checkQuesBodyFragment
        }
    }

    override fun initView(view: View) {

    }

    override fun initData() {
        if (ConstantBean.constantExamBean == null) {
            return
        }
        val kindBeanList = ConstantBean.constantExamBean!!.TypeList
        typeIndex = requireArguments().getInt(AnswerConstant.KEY_TYPEINDEX)
        quesIndex = requireArguments().getInt(AnswerConstant.KEY_QUESINDEX)
        childIndex = requireArguments().getInt(AnswerConstant.KEY_CHILDINDEX)
        var loadChildIndex = requireArguments().getInt(AnswerConstant.KEY_LOADCHILDINDEX)
        Log.e("填空题", "loadChildIndex:" + loadChildIndex)
        ques = kindBeanList!![typeIndex].QuesList!![quesIndex]
        sortIndexType = ques.ChildList!![0].SortIndexType!!
        childIndex = ques.currentLoadChildIndex
        defaultLoadIndex = ques.ChildList!![childIndex].currentLoadItemIndex
        itemSortIndex = ques.ChildList!![childIndex].ItemList!![defaultLoadIndex].ItemSortIndex!!
        setInteractWebView()
    }

    override fun getLayoutId(): Int {
        return R.layout.ev_view_looking_ques_body
    }

    //加载题干
    private fun setInteractWebView() {
        val askBody = ques.ChildList!![childIndex].ChildAsk
        mBinding.interactWv.initWebView(
            requireActivity(),
            askBody!!,
            object : InteractWebView.Callback {
                override fun finishCallback() {
                    if (ConstantBean.taskControlBean != null) {
                        if (ConstantBean.taskControlBean!!.trainSence == 1 || ConstantBean.taskControlBean!!.trainSence == 2
                            || ConstantBean.taskControlBean!!.trainSence == 3
                        )//考试定制化需求，不显示【析】
                            mBinding.interactWv.showOrHideAnalysisButton(0)
                    } else {
                        mBinding.interactWv.showOrHideAnalysisButton(1)
                    }
                    wvLoadUrl(itemSortIndex)
                    if (getRealItem() != null) {
                        showResultView(getRealItem()!!)
                    }
                    adjustFontSize()
                }

                override fun clickCallback(position: String) {
                    itemSortIndex = position
                    wvLoadUrl(itemSortIndex)
                    if (getRealItem() != null) {
                        showResultView(getRealItem()!!)
                    }
                }
            }
        )

    }

    //获取Item
    fun getRealItem(): Item? {
        when (sortIndexType) {
            "0" -> {
                ques.ChildList!!.forEachIndexed { index, child ->
                    if (child.ItemList!![0].ItemSortIndex == itemSortIndex) {
                        return child.ItemList!![0]
                    }
                }
            }
            "1" -> {
                ques.ChildList!![0].ItemList!!.forEachIndexed { index, item ->
                    if (item.ItemSortIndex == itemSortIndex) {
                        return item
                    }
                }
            }
        }
        return null
    }

    //获取序号在ItemList 中的位置
    fun getRealIndex(): Int {
        when (sortIndexType) {
            "0" -> {
                ques.ChildList!!.forEachIndexed { index, child ->
                    if (child.ItemList!![0].ItemSortIndex == itemSortIndex) {
                        return index
                    }
                }
            }
            "1" -> {
                ques.ChildList!![0].ItemList!!.forEachIndexed { index, item ->
                    if (item.ItemSortIndex == itemSortIndex) {
                        return index
                    }
                }
            }
        }
        return 0
    }

    /**
     *   答题点下划线，高亮等等绘制
     *   词汇匹配'm'需要展示标准答案
     */
    private fun wvLoadUrl(position: String = "-1") {
        mBinding.interactWv.scrollPosition(position)
        when (sortIndexType) {
            "0" -> { //小题抽题
                var showItemAnswer = (ques.TypeNo == "m" || ques.TypeNo == "k")
                ques.ChildList!!.forEachIndexed { index, child ->
                    if (child.ItemList!![0].ScanImagePath.isNullOrEmpty()) {
                        mBinding.interactWv.loadUrl(
                            child.ItemList!![0],
                            position == child.ItemList!![0].ItemSortIndex.toString(),
                            isAnswer = false,
                            showItemAnswer
                        )
                    } else {
                        mBinding.interactWv.loadImageUrl(requireActivity(), child.ItemList!![0])
                    }
                }
            }
            "1" -> { //答题点抽题
                var showItemAnswer = (ques.TypeNo == "m" || ques.TypeNo == "k")
                ques.ChildList!![0].ItemList!!.forEachIndexed { index, item ->
                    if (item.ScanImagePath.isNullOrEmpty()) {
                        mBinding.interactWv.loadUrl(
                            item,
                            position == item.ItemSortIndex.toString(),
                            isAnswer = false,
                            showItemAnswer
                        )
                    } else {
                        mBinding.interactWv.loadImageUrl(requireActivity(), item)
                    }
                }
            }
        }
    }

    fun showResultView(item: Item) {
        if (view == null) {
            return
        }
        val fillContextAnswerResultView: FillContextAnswerResultView =
            mBinding.resultView.root.findViewById(R.id.fill_context_answer)
        val typeNo = ques.TypeNo
        fillContextAnswerResultView.setFillResult(item)
        mBinding.resultView.root.findViewById<View>(R.id.ll_close)
            .setOnClickListener { v: View? -> mBinding.resultView.root.visibility = View.GONE }
        if (ConstantBean.taskControlBean!!.expired) {
            fillContextAnswerResultView.showAnalysisAndAnswer()
        }
        if (TextUtils.equals("m", typeNo)) { //匹配题只显示答案解析
            fillContextAnswerResultView.onlyShowAnalysis()
        }

        val ctrlAnimation = TranslateAnimation(
            TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, 0f,
            TranslateAnimation.RELATIVE_TO_SELF, 1f, TranslateAnimation.RELATIVE_TO_SELF, 0f
        )
        ctrlAnimation.duration = 400
        mBinding.resultView.root.visibility = View.VISIBLE
        mBinding.resultView.root.startAnimation(ctrlAnimation)
        Handler().postDelayed({ mBinding.interactWv.scrollPosition(itemSortIndex) }, 500)
    }

    fun setCurrentItem(itemIndex: Int) {
        itemSortIndex = this.ques.ChildList!![0].ItemList!![itemIndex].ItemSortIndex!!
        wvLoadUrl(itemSortIndex)
        if (mBinding.resultView.root.visibility == View.VISIBLE) {
            if (getRealItem() != null) {
                showResultView(getRealItem()!!)
            }
        }
    }

    fun adjustFontSize() {
        if (mBinding.interactWv != null) {
            mBinding.interactWv.setFontSize()
        }
    }
}
